<!--// 歌手详情页面-->
<template>
  <transition name="slide">
      <musicList :bg-image="bgImage" :songs="songs" :title="title"></musicList>
  </transition>

</template>

<script type="text/ecmascript-6">
  import {mapGetters} from 'vuex'
  import {getSingerDetail, getSongVkey} from '../../api/singer'
  import {ERR_OK} from '../../api/config'
  import {createSong} from '../../common/js/song'
  import musicList from '../../components/music-list/music-list'

  export default {
    data() {
      return {
        songs: []
      }
    },
    computed: {
      title() {
        return this.singer.name
      },
      bgImage() {
        return this.singer.avatar
      },
      // 取数据(vuex)
      ...mapGetters([
        'singer'
      ])
    },
    created() {
      // 得到抓取数据
      this._getdetail()
    },
    methods: {
      // 得到歌手详情
      _getdetail() {
        // 在当前歌手页页面进行刷新的时候，是没有数据的
        // 让用户返回上一级重新进行选择
        if (!this.singer.id) {
          // 返回上一级
          this.$router.push('/singer')
          return
        }
        // 类似于ajax请求数据
        getSingerDetail(this.singer.id).then((res) => {
          if (res.code === ERR_OK) {
            this.songs = this._normalizeSongs(res.data.list)
          }
        })
      },
      // 对歌曲数据进行处理
      _normalizeSongs (list) {
        let ret = []
        list.forEach((item) => {
          let {musicData} = item
          getSongVkey(musicData.songmid).then((res) => {
            const vkey = res.data.items[0].vkey
            if (musicData.songid && musicData.albummid) {
              ret.push(createSong(musicData, vkey))
            }
          })
        })
        return ret
      }
    },
    components: {
      musicList
    }
  }
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">

  .slide-enter-active, .slide-leave-active
    transition: all 0.3s
  .slide-enter, .slide-leave-to
    transform: translate3d(100%, 0, 0)
</style>
